var express = require('express');
var router = express.Router();

var selectFields = '-password -token';

/* 获取 */
router.get('/', function (req, res, next) {
	
	var limit = req.query.limit;
	var skip = req.query.skip;
	
	var search = {};
	if (req.query.phone) {
		search.phone = new RegExp(req.query.phone, 'i');
	}
	if (req.query.name) {
		search.name = new RegExp(req.query.name, 'i');
	}
	if (req.query.roles) {
		if (Array.isArray(req.query.roles)) {
			search.roles = {
				$in : req.query.roles
			};
		} else {
			search.roles = {
				$in : [req.query.roles]
			};
		}
	}
	
	Db.Master.count(search, function(err, count) {
		if (err) {
			res.send(500, err);
			return;
		}
		Db.Master.find(search, selectFields, {
			limit : limit,
			skip : skip
		}, function(err, masters) {
			if (err) {
				res.send(500, err);
				return;
			}
			
			res.send({
				count : count,
				list : masters,
				roles : BASE_CONFIG.roles
			});
		}).sort({
			createTime : 'desc'
		});
	});
});
router.get('/:id', function (req, res, next) {
	var id = req.params.id;
	
	Db.Master.findById(id, selectFields, function(err, master) {
		if (err) {
			res.send(500, err);
			return;
		}
		
		if (master) {
			res.send(master);
		} else {
			res.send(404);
		}
	});
});

/* 新增 */
router.post('/', function (req, res, next) {
	var master = new Db.Master(req.body);
	
	var err = master.validateSync();
	if (err) {
		res.send(500, err);
		return;
	}
	
	master.password = createPassword(master.password);
	
	master.save(function(err) {
		if (err) {
			res.send(500, err);
			return;
		}
		
		res.send(master._id);
	});
});

/* 修改 */
router.put('/:id', function(req, res, next) {
	var id = req.params.id;
	Db.Master.findById(id, function(err, master) {
		if (err) {
			res.send(500, err);
			return;
		}
		
		if (master) {
			for (var path in req.body) {
				master.set(path, req.body[path]);
			}
	
			var err = master.validateSync();
			if (err) {
				res.send(500, err);
				return;
			}
	
			if (req.body.password) {
				master.password = createPassword(master.password);
			}
			
			master.save(function(err) {
				if (err) {
					res.send(500, err);
					return;
				}
				res.send(master);
			});
			
		} else {
			res.send(404);
		}
	});
});

/* 修改密码 */
router.put('/:id/password', function(req, res, next) {
	var id = req.params.id;
	Db.Master.findById(id, function(err, master) {
		if (err) {
			res.send(500, err);
			return;
		}
		
		if (master) {
			if (master.password != createPassword(master.oldPassword)) {
				master.invalidate('password', '原密码错误。', master.oldPassword);
				var err = master.validateSync();
				if (err) {
					res.send(400, err);
					return;
				}
			}
			
			master.password = req.body.password;
			
			var err = master.validateSync();
			if (err) {
				res.send(500, err);
				return;
			}
	
			master.password = createPassword(master.password);
			
			master.save(function(err) {
				if (err) {
					res.send(500, err);
					return;
				}
				
				res.send(master);
			});
		} else {
			res.send(404);
		}
	});
});


module.exports = router;